﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace FileViewer
{
    class FileViewer
    {
        ListView L_View;
        FileManager F_Manager;

        private FileViewer()
        { 
        }

        public FileViewer(Control Owner, string baseDir)
        {
            L_View = new ListView();
            L_View.SelectedIndexChanged += L_View_SelectedIndexChanged;
            L_View.Width = 300;
            L_View.Height = 500;
            L_View.Parent = Owner;

            L_View.View = View.List;
            L_View.Font = new Font("Arial", 10);



            F_Manager = new FileManager(baseDir);
            Show();
        }

        void Show()
        {
            L_View.Items.Clear();
            ImageList C = new ImageList();
            L_View.LargeImageList = C;
            L_View.SmallImageList = C;

            if (F_Manager.History.Count > 1)
            {
                C.Images.Add(Image.FromFile(@"F:\Users\Nekseron\Desktop\артем\vrn-file-manager\FileViewer\Image\4.png"));
                L_View.Items.Add(new ListViewItem("<-..", C.Images.Count - 1)); 
            }

            for (int i = 0; i < F_Manager.Files.Count; i++)
            {
                System.Drawing.Icon ic;
                try
                {
                    ic = System.Drawing.Icon.ExtractAssociatedIcon(F_Manager.NowDirectory + "\\" + F_Manager.Files[i]);
                    C.Images.Add(ic);
                    L_View.Items.Add(new ListViewItem(F_Manager.Files[i], C.Images.Count - 1));  
                }
                catch
                {
                    try
                    {
                        C.Images.Add(Image.FromFile(@"F:\Users\Nekseron\Desktop\артем\vrn-file-manager\FileViewer\Image\4.png"));
                        L_View.Items.Add(new ListViewItem(F_Manager.Files[i], C.Images.Count - 1));
                    }
                    catch
                    {
                        int y = 7;
                    }
                }
                    
                                      
                

            }
        }

        private void L_View_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (L_View.SelectedItems.Count == 1)
            {
                if (L_View.SelectedItems[0].Text == "<-..")
                    F_Manager.GoBack(); 
                else
                    F_Manager.Open(L_View.SelectedItems[0].Text);

                Show();
            }
        }


    }
}
